package com.daifeng.authserver.controller;

import com.daifeng.authserver.vo.UserRegVo;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@Controller
public class regController {

    /**
     * 用户注册服务
     * @param userRegVo
     * @param result
     * @param redirectAttributes
     * @return
     */
    @RequestMapping("/reg/SaveUser")
    public String SaveUser(@Valid UserRegVo userRegVo, BindingResult result, RedirectAttributes redirectAttributes){
        if (result.hasErrors()) { //判断是否通过校验
            Map<String, String> map = new HashMap<>();
            result.getFieldErrors().forEach((item) -> {
                //FieldError 获取到错误提示
                String message = item.getDefaultMessage();
                //获取错误的属性的名字
                String field = item.getField();
                map.put(field, message);
            });
            redirectAttributes.addFlashAttribute("data",map);
        } else {
            // 调用远程注册服务

        }
           return "redirect:http://auth.gulimall.com/reg.html";
    }
}
